package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.MathUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import de.greenrobot.event.EventBus;

/**
 * Created by wongqi on 16/6/23.
 */
public class D3BD1Fragment extends MostLotteryFragment {
    private int last;
    private static final int[] notes = {1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 63, 69, 73, 75, 75
            , 73, 69, 63, 55, 45, 36, 28, 21, 15, 10, 6, 3, 1};
    private final Bet bet = new Bet(EnumCode.gameId.D3);

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setupBalls(null, 0, notes.length);
        bet.setPlayType(EnumCode.PlayType.D3_DXBD);
    }

    @Override
    protected void onSetMarkText(View parent, int groupIndex) {
        TextView title = (TextView) parent.findViewById(R.id.title_mark);
        TextView tip = (TextView) parent.findViewById(R.id.tip_mark);
        title.setText("单选包点");
        tip.setText("请选择一个号码");
    }

    @Override
    protected void onClearChoices() {
        super.onClearChoices();
        last = -1;
    }

    @Override
    public boolean onRandomBalls() {
        onClearChoices();
        int r = MathUtils.random(notes.length);
        mGrid.setItemChecked(getAdapter().transToRawPosition(r, 0), true);
        return true;
    }

    @Override
    protected int onSetGroupColor(int groupIndex) {
        return red;
    }

    @Override
    protected boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index) {
        if (checked) {
            if (last != -1)
                parent.setItemChecked(getAdapter().transToRawPosition(last, 0), false);
            last = position;
        } else {
            if (position == last)
                last = -1;
        }
        if (last >= 0) {
            bet.getReds().clear();
            bet.getReds().add(ballText);
            bet.setWagerCount(notes[position]);
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent());
        }
        return true;
    }
}
